package org.aptech.hdax.easybuy.service.impl;

import org.aptech.hdax.easybuy.dao.ProductDao;
import org.aptech.hdax.easybuy.pojo.Product;
import org.aptech.hdax.easybuy.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Product> findAll(Product condition) {
        return productDao.findAll(condition);
    }

    @Override
    public Product get(Integer id) {
//        redisTemplate.delete("hjafajf");
        //先从redis缓存中来获取，如果没有，调用数据访问层方法
        Product product = (Product) redisTemplate.opsForValue().get("product-"+id);
        if (product == null){
            product = productDao.get(id);
            redisTemplate.opsForValue().set("product-"+id, product);
        }
        return product;
    }
}
